package de.hdu.pms.ho.ctrl;



import org.apache.log4j.Logger;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import de.hdu.pms.ho.dao.KategorieDao;
import de.hdu.pms.ho.dao.ZimmerDao;
import de.hdu.pms.ho.model.Kategorie;
import de.hdu.pms.ho.model.Zimmer;

/**
 * Controller for editing, saving, deleting and showing all objects of {@link Zimmer}
 * 
 * @author Helena Semmel
 *
 */


@Controller
public class ZimmerController {
	private static Logger Log = Logger.getLogger(ZimmerController.class);
	public static void setLog(Logger log) {
		Log = log;
	}
	
	private ZimmerDao zimmerDao;
	public void setZimmerDao(ZimmerDao zimmerDao) {
		this.zimmerDao = zimmerDao;
	}
	public ZimmerDao getZimmerDao() {
		return zimmerDao;
	}
	
	private KategorieDao kategorieDao = null;
	public KategorieDao getKategorieDao() {
		return kategorieDao;
	}
	public void setKategorieDao(KategorieDao kategorieDao) {
		this.kategorieDao = kategorieDao;
	}

	
	/**
	 * Creates a new {@link Zimmer} or updates an existing one
	 * @param id optional, only for update
	 * @return
	 */
	@RequestMapping(value="/EditZimmer.html", method=RequestMethod.GET)
    public ModelAndView edit(@RequestParam(required=false) Integer id) {
		Log.debug("EditZimmer.html");
    	ModelAndView mv = new ModelAndView();
   		
			if (id == null) {	
				mv.addObject("message", "Zimmer anlegen");
				mv.addObject("kategorien", kategorieDao.findAll());
				mv.addObject(new Zimmer());
			} else {
				mv.addObject("message", "Zimmer ändern");
				mv.addObject(zimmerDao.findById(id));
				mv.addObject("kategorien", kategorieDao.findAll());
			}
   		mv.addObject("title", "Zimmer");
   		mv.addObject("edit", true);
   		mv.setViewName("protected/edit-zimmer");
    	return mv;
    }

	

	/**
	 * Saves a {@link Zimmer} in the database
	 * @param id the primary key of {@link Zimmer}
	 * @param nr the room number
	 * @param betten the number of beds
	 * @param qm the square meter
	 * @param haustier if pets are allowed
	 * @param raucher if smoking is allowed
	 * @param kategorie the foreign key to {@link Kategorie}
	 * @return redirect to the list of all existing rooms
	 */
	@RequestMapping(value="SaveZimmer.html", method=RequestMethod.POST)   
	public String save(@RequestParam(required=false) Integer id, Integer nr, Integer betten, 
			Integer qm, Boolean haustier, Boolean raucher, Integer kategorie) {
		Log.debug("SaveZimmer.html");
		Kategorie k = getKategorieDao().findById(kategorie);	
		
			if(id != null) {
				getZimmerDao().modify(id, nr, betten, qm, haustier, raucher, k);	//bei Änderung
					
			} else{
				getZimmerDao().create(nr, betten, qm, haustier, raucher, k);		//beim Neuanlegen
				
				}
		return "redirect:AlleZimmer.html";
	}
	
	/*@RequestMapping(value="SaveZimmer.html", method=RequestMethod.POST)   
	public String save( Zimmer zimmer, Integer kategorie) {
		Log.debug("SaveZimmer.html");
		Kategorie k = kategorieDao.findbyId(kategorie);	
		zimmer.setKategorie(k);
		zimmerDao.save(zimmer);
		//model.addAttribute("statusMessageKey", "zimmer.form.msg.success");
		
		return "redirect:AlleZimmer.html";
	}*/
	
	
	/**
	 * Deletes a {@link Zimmer} object from the database
	 * @param zimmer that should be deleted with its primary key
	 * @param id primary key of the object
	 * @return redirect to the list of all rooms
	 * returns an exception, if the room is already assigned to a category
	 */
	@RequestMapping(value="/LoescheZimmer.html", method =RequestMethod.GET)
	public ModelAndView delete(Zimmer zimmer, Integer id) {
		try {
			zimmerDao.deletebyId(id);
			return new ModelAndView("redirect:AlleZimmer.html");
		} catch (DataIntegrityViolationException e) {					    //Exception, falls Zimmer bereits bei Arbeitsplan oder Reservierung
			ModelAndView mv = new ModelAndView("protected/errordelete");	//verwendet worden ist, dann kann es nicht gelöscht werden
			mv.addObject("message", e.getMessage());  
			return mv;
		}
	}
	
	/**
	 * Returns all existing {@link Zimmer} objects from the database
	 * @return a list of {@link Zimmer}
	 */
	@RequestMapping(value="/AlleZimmer.html", method=RequestMethod.GET)
	public ModelAndView findAll() {
		ModelAndView mv = new ModelAndView();
		mv.addObject("title", "Zimmer");
		mv.addObject("message", "Alle vorhandenen Zimmer");
		mv.addObject("zimmer1", zimmerDao.findAll());
		mv.addObject("edit", true);
		mv.setViewName("protected/list-zimmer");
		return mv;
	}
	
	
	
	/**
	 * Returns all existing {@link Zimmer} objects with its category and price.
	 * Needed for the customer view
	 * @return a list of {@link Zimmer}
	 */
	@RequestMapping(value="/AlleZimmermitKat.html", method=RequestMethod.GET)
	public ModelAndView findAllmitKat() {
		ModelAndView mv = new ModelAndView();
		mv.addObject("title", "Zimmer");
		mv.addObject("message", "Alle vorhandenen Zimmer");
		mv.addObject("zimmer1", zimmerDao.findAll());
		//mv.addObject("edit", true);
		mv.setViewName("open/list-allezimmermitkat");		//view für Kundensicht mit Kat. und Preis
		return mv;
	}
	
}













